package com.aaa.controller;

import com.aaa.entity.Album;
import com.aaa.entity.Emp;
import com.aaa.entity.Type;
import com.aaa.entity.Voice;
import com.aaa.service.AlbumService;
import com.aaa.service.TypeService;
import com.aaa.service.VoiceService;
import com.aaa.util.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



@Controller
@RequestMapping("album")
public class AlbumController {
    @Resource
    AlbumService albumService;
    @Resource
    TypeService typeService;
    @Resource
    VoiceService voiceService;
    @RequestMapping("selectAlbum")
    @ResponseBody
    public PageInfo select(Integer pageNum, Integer pageSize, String aname) {
        if(aname!=null){
            //模糊查询专辑名根据
            PageHelper.startPage(pageNum,pageSize);
            List<Album> list=albumService.selectAlbum(aname);
            PageInfo pageInfo=new PageInfo(list);
            return  pageInfo;
        }else {
            //查询全部
            PageHelper.startPage(pageNum,pageSize);
            List<Album> list=albumService.selectAlbumAll();
            PageInfo pageInfo=new PageInfo(list);
            return  pageInfo;
        }

    }
    //给音频添加下拉框赋值的查询
    @RequestMapping("selectAlbumAll")
    @ResponseBody
    public List<Album> selectAlbumAll() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("state",1);
        return albumService.list(queryWrapper);
    }
    //批量删除
    @RequestMapping("delAlbum")
    @ResponseBody
    public boolean delAlbum(String str) {
        boolean b=false;
    String[] aid= str.split(",");
    List<String> aidList=new ArrayList();
    for (int i=0;i<aid.length;i++){
        aidList.add(aid[i]);
    }
     QueryWrapper queryWrapper=new QueryWrapper();
     queryWrapper.in("aid",aidList);
     List<Voice> list = voiceService.list(queryWrapper);
     if(list.size()!=0){
         b=false;
     }else {
         for (int i = 0; i < aid.length; i++) {
             b = albumService.removeById(aid[i]);
         }
     }
    return b;
    }
    @RequestMapping("updAlbum")
    @ResponseBody
    public boolean updEmp(Album album,Integer aid) {
        boolean b = albumService.updateById(album);

        return true;
    }
    //封面图片上传
    @RequestMapping("upload")
    @ResponseBody
    public boolean upload(@RequestParam("file") MultipartFile file, Integer aid, HttpServletRequest request) {
    String  cover= FileUtil.upload(file,"",request);
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("aid",aid);
        updateWrapper.set("cover",cover);
        boolean update = albumService.update(updateWrapper);
        return  update;
    }
    //专辑添加
    @RequestMapping("albumAdd")
    @ResponseBody
    public boolean albumAdd(Album album) {
        album.setCover("yyx.gif");
        album.setState(3);
        album.setUploadid(2);
        album.setCid(1);
        //密码编译
        boolean result=albumService.save(album);
        return result;
    }
    @RequestMapping("selectType")
    @ResponseBody
    public List<Type> selectType(){
        List<Type> list = typeService.list();
        return list;
    }

}
